﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Xml.Linq;

public partial class Administrator_Videos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        loadVideos();
    }
    private void loadVideos()
    {
        try
        {
            XDocument xml = XDocument.Load(Server.MapPath("~/App_Data/VideoClips.xml"));
            var data = from o in xml.Descendants("clip")
                       select new
                       {
                           title = o.Element("title").Value,
                           link = o.Element("linkyt").Value
                       };
            if (data.Count() > 0)
            {
                rptVideos.DataSource = data.ToList();
                rptVideos.DataBind();
            }
        }
        catch (Exception)
        {
        }
    }
    private bool InsertXML(string path, string fileName, string title)
    {
        try
        {
            DataRow dr;
            DataSet dt = new DataSet();
            dt.ReadXml(path);
            dr = dt.Tables[0].NewRow();
            dr["title"] = title;
            dr["linkyt"] = fileName;
            dt.Tables[0].Rows.Add(dr);
            dt.WriteXml(path, XmlWriteMode.WriteSchema);
        }
        catch
        {
        }
        return true;
    }
    private bool checkExist(string title)
    {
        bool value = false;
        try
        {
            DataSet dt = new DataSet();
            dt.ReadXml(Server.MapPath("~/App_Data/VideoClips.xml"));
            foreach (DataRow dr in dt.Tables[0].Rows)
            {
                if (dr["title"] == title)
                {
                    value = true;
                    break;
                }
            }
        }
        catch(Exception)
        {
        }
        return value;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtLinkfile.Text != "" || file.FileName != null && txtTitle.Text != "")
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    if (txtLinkfile.Text != "")
                    {
                        if (checkExist(txtTitle.Text))
                            lblmsg.Text = "This title exitsted";
                        else
                        {
                            if (InsertXML(Server.MapPath("~/App_Data/VideoClips.xml"), txtLinkfile.Text, txtTitle.Text))
                                lblmsg.Text = "Save complete";
                            else
                                lblmsg.Text = "Save error";
                        }
                    }
                    else if (file.FileName != null)
                    {
                        string extention = System.IO.Path.GetExtension(file.FileName);
                        if (extention.ToUpper() != ".FLV" || extention.ToUpper() != ".FLV" || extention.ToUpper() != ".WMA" || extention.ToUpper() != ".AVI")
                            lblmsg.Text = "Allow extention : flv, mp4, wma, avi";
                        else
                        {
                            file.SaveAs(Server.MapPath("Webdirectory/Videos/") + file.FileName);
                            if (InsertXML(Server.MapPath("~/App_Data/VideoClips.xml"), Server.MapPath("Webdirectory/Videos/") + file.FileName, txtTitle.Text))
                                lblmsg.Text = "Save complete";
                            else
                                lblmsg.Text = "Save error";
                        }
                    }
                }
            }
            else
                lblmsg.Text = "Enter title and file path.";
        }
        catch (Exception)
        {
        }
    }
    protected void rptVideos_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "delete")
            {
                panelVideo.Visible = true;
            }
            if (e.CommandName == "edit")
            {
                panelVideo.Visible = true;
            }
        }
        catch (Exception)
        {
        }
    }
}